package com.uchengwang.ui.base_ui.basefm;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebChromeClient;
import android.widget.FrameLayout;

import com.uchengwang.R;
import com.uchengwang.functions.http.config.BaseConfig;
import com.uchengwang.ui.UChengApplication;
import com.uchengwang.ui.base_ui.ActivityTitleBar;
import com.uchengwang.ui.cm_ui.cycle.bridge.WebViewJsHandler;
import com.uchengwang.ui.cm_ui.cycle.ui.LoadCycleWebHtmlActivity;
import com.uchengwang.ui.cm_ui.release.ui.FastReleaseActivity;
import com.uchengwang.ui.self.jsbridge.BridgeWebView;
import com.uchengwang.ui.self.jsbridge.DefaultHandler;

/**
 * Created by U城网 on 2018/2/8.
 * 首页社圈
 */

public class FragmentCycle extends BaseFragment{
    private View view;
    private BridgeWebView bridgeWebView;
    private ActivityTitleBar titleBar;
    private FrameLayout fm_ln;
    private FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        setLayoutId(R.layout.fm_cycle_layout);
        view = super.onCreateView(inflater, container, savedInstanceState);

        initView();
        return view;
    }

    private void initView() {
        titleBar = view.findViewById(R.id.fm_titleBar);
        titleBar.setTitle("社圈");
        titleBar.setLeftVisible(false);
        titleBar.setRightText("发布", new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(FragmentCycle.this.getContext(), FastReleaseActivity.class));
            }
        });
        titleBar.setLeftView("广场", 0, 0, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(FragmentCycle.this.getContext(), LoadCycleWebHtmlActivity.class);
                intent.putExtra(BaseConfig.URL, BaseConfig.BaseWebURL+"/cycle/square");
                intent.putExtra(BaseConfig.URL_TITLE, "广场");
                startActivity(intent);
            }
        });
        fm_ln =view.findViewById(R.id.fm_fl);
        bridgeWebView = new BridgeWebView(getContext());
        fm_ln.addView(bridgeWebView, 0);
//        bridgeWebView = (BridgeWebView) view.findViewById(R.id.fm_webView);

        bridgeWebView.setDefaultHandler(new DefaultHandler());
        bridgeWebView.setWebChromeClient(new WebChromeClient());

        /**
         * js发送给按住消息   submitFromWeb 是js调用的方法名    安卓\返回给js
         */
//        LoadingUtils.getUtils().loading(getActivity());
        WebViewJsHandler handler = WebViewJsHandler.getHandler();
        handler.registerWebviewJsHandlers(getActivity(), bridgeWebView);

        System.out.println("社圈首页打开:"+handler);

        bridgeWebView.loadUrl(BaseConfig.BaseWebURL + "/home");
    }

    @Override
    public void onResume() {
        super.onResume();
        if (BaseConfig.isRelease || UChengApplication.isCycleDelete){
            bridgeWebView.reload();
        }
    }

    @Override
    public void onPause() {
        super.onPause();
        BaseConfig.isRelease = false;
        UChengApplication.isCycleDelete = false;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if(bridgeWebView != null){
            bridgeWebView.removeAllViews();
            bridgeWebView.destroy();

            fm_ln.removeAllViews();
            fm_ln = null;
        }
    }
}
